<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/4/12
     * Time: 下午4:04
     */

    namespace app\common\traits;

    use think\exception\HttpResponseException;

    use think\facade\Config;
    use think\facade\Log;
    use think\Response;

    trait apiinit
    {

        static public $AuthTokenStatus = 0;

        //数据集合！
        protected $_initialize = [];

        /**
         * 存储数据到数据集
         *
         * @param mixed  $name
         * @param string $value
         */
        protected function assign($name,$value = '')
        {

            $this->_initialize[$name] = $value;
            return $this;
        }

        /**
         * 输出数据
         *
         * @param string $template
         * @param array  $vars
         * @param array  $replace
         * @param array  $config
         *
         * @return mixed|void
         */
        protected function fetch($template = '',$vars = [],$replace = [],$config = [])
        {

            if ($template !== '')
            {
                $this->_initialize['template'] = $template;
            }


            $this->result($this->_initialize,200,$msg = '',$type = 'json');
        }

        /**
         * 操作错误跳转的快捷方法
         *
         * @param string $msg
         */
        protected function error($msg = 'error',$url = null,$data = '',$wait = 3,array $header = [])
        {

            $this->result($data,500,$msg,'json');
        }

        /**
         * 操作成功跳转的快捷方法
         *
         * @param string $msg
         */
        protected function success($msg = 'success',$url = null,$data = '',$wait = 3,array $header = [])
        {

            $this->result($data,200,$msg,'json');
        }

        /**
         * 返回封装后的 API 数据到客户端
         *
         * @access protected
         *
         * @param mixed  $data   要返回的数据
         * @param int    $code   返回的 code
         * @param mixed  $msg    提示信息
         * @param string $type   返回数据格式
         * @param array  $header 发送的 Header 信息
         *
         * @return void
         * @throws HttpResponseException
         */
        protected function result($data = [],$code = 200,$msg = 'success',$type = 'json',array $header = [])
        {

            Log::record(json_encode(['titile' => '日志','msg' => $msg,'']));

            $result = [
                'code' => $code,
                'msg'  => $msg,
                'time' => \think\facade\Request::time(),
                'data' => $data,
            ];

            if (Config::get('app_debug') && \request()->domain() != 'https://appvu.ssjtime.cn')
            {
                $result['debug'] = static::getLog();
            }

            throw new HttpResponseException(Response::create($result,$type)->header(['auth-token-status' => static::$AuthTokenStatus]));
        }

        static public function getLog()
        {

            return $Log_sql = Log::getLog();
        }

    }